package com.dawn.usermanage.serviceimpl;

import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import weibo4j.Tag;
import weibo4j.User;
import weibo4j.Weibo;
import weibo4j.WeiboException;

import com.dawn.usermanage.bean.TwSinainfo;
import com.dawn.usermanage.bean.UserBean;
import com.dawn.usermanage.dao.SinaUserMDao;
import com.dawn.usermanage.service.SinaUserMService;
import com.dawn.usermanage.service.UserManageService;
import com.dawn.utils.exceptions.DaoAccessException;

@Service
public class SinaUserMServiceImpl implements SinaUserMService {

    private static Logger logger = Logger.getLogger(SinaUserMServiceImpl.class);

    @Autowired
    private SinaUserMDao sinaUserMDao;
    @Autowired
    private UserManageService userManageService;

    @Override
    public TwSinainfo findSUserInfo(String sinaId) {
        List<TwSinainfo> sinaUsers = sinaUserMDao.findSinaUser(sinaId);
        if (!sinaUsers.isEmpty()) {
            TwSinainfo sinaUser = sinaUsers.get(0);
            return sinaUser;
        }
        return null;
    }

    @Override
    public UserBean addUserInfo(UserBean userBean) {
        Date date = new Date();
        UserBean user = null;
        userBean.setCreateTime(date);
        userBean.setLastLoginTime(date);
        try {
            sinaUserMDao.addUserInfo(userBean);
            user = userManageService.findUserBySinaId(userBean.getTwSinainfo().getSinaId());
        } catch (Exception e) {
            logger.error("Data access error");
            e.printStackTrace();
            throw new DaoAccessException();
        }
        return user;
    }

    @Override
    public boolean updateSinaBasic(String sinaId, String accessToken, String accessTokenSecret) {
        TwSinainfo sinaUser = sinaUserToLocal(sinaId, accessToken, accessTokenSecret);
        try {
            sinaUserMDao.updateSinaBasic(sinaUser);
        } catch (Exception e) {
            logger.error("Data access error");
            e.printStackTrace();
            throw new DaoAccessException();
        }
        return true;
    }

    @Override
    public TwSinainfo addSinaBasic(String sinaId, String accessToken, String accessTokenSecret) {
        TwSinainfo sinaUser = sinaUserToLocal(sinaId, accessToken, accessTokenSecret);
        try{
            sinaUserMDao.addSinaUser(sinaUser);
        }
        catch(Exception e){
            logger.error("Data access error");
            e.printStackTrace();
            throw new DaoAccessException();
        }
        return sinaUser;
    }

    private TwSinainfo sinaUserToLocal(String sinaId, String accessToken, String accessTokenSecret) {
        TwSinainfo sinaUser = new TwSinainfo();
        try {
            Weibo weibo = new Weibo();
            weibo.setToken(accessToken, accessTokenSecret);
            User user = weibo.showUser(sinaId);
            sinaUser.setSinaId(Long.toString(user.getId()));
            sinaUser.setScreenName(user.getName());
            sinaUser.setFansNum(user.getFollowersCount());
            sinaUser.setFollowNum(user.getFriendsCount());
            sinaUser.setStatusesCount(user.getStatusesCount());
            sinaUser.setGender(user.getGender());
            sinaUser.setCreatedAt(user.getCreatedAt());
            sinaUser.setDescription(user.getDescription());
            sinaUser.setProfileBackgroundImageUrl(user.getProfileBackgroundImageUrl());
            sinaUser.setProfileBackgroundTile(user.getProfileBackgroundTile());
            sinaUser.setProfileImageUrl(user.getProfileImageURL() == null ? "" : user.getProfileImageURL().toString());
            sinaUser.setVerified(user.isVerified());
            sinaUser.setUrl(user.getURL() == null ? "" : user.getURL().toString());
            List<Tag> gettags=weibo.gettags(sinaId);
            String tags="";
        	for(Tag status : gettags) {
        		//System.out.println( status.toString());
        		tags+=status.getValue()+" ";
        	}
            sinaUser.setTags(tags);
        } catch (WeiboException e) {
            e.printStackTrace();
            return null;
        }
        return sinaUser;
    }

}
